<template>
  <div class="app-container">
    <PageSearch :showSearch.sync="showSearch" :getList="getList" :handleQuery="handleQuery" :resetQuery="resetQuery">
      <template #pagination>
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </template>
      <template #form>
        <el-form :model="queryParams" ref="queryForm" size="small" inline v-show="showSearch">
          <el-form-item prop="userName">
            <el-input
              v-model="queryParams.userName"
              placeholder="用户名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item prop="nickName">
            <el-input
              v-model="queryParams.nickName"
              placeholder="用户昵称"
              clearable
              style="width: 240px"
              @keyup.enter.native="getUnAllocateList"
            />
          </el-form-item>
          <el-form-item prop="phonenumber">
            <el-input
              v-model="queryParams.phonenumber"
              placeholder="手机号码"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-form>
      </template>
      <template #actions>
        <el-button
          size="mini"
          plain
          type="primary"
          @click="handleAdd"
          v-hasPermi="['distribution:user:insertPlatformUser']"
        >
          创建平台用户
        </el-button>
        <el-button
          size="mini"
          type="primary"
          @click="roleManage.show = true"
          v-hasPermi="['distribution:roleDistr:list']"
        >
          平台角色管理
        </el-button>
      </template>
    </PageSearch>

    <el-table :height="calTableHeight()" v-loading="loading" ref="TableRef" :data="tableData">
      <el-table-column label="用户名称" prop="userName" :show-overflow-tooltip="true" />
      <el-table-column label="用户昵称" prop="nickName" :show-overflow-tooltip="true" />
      <el-table-column label="邮箱" prop="email" :show-overflow-tooltip="true" />
      <el-table-column label="手机" prop="phonenumber" :show-overflow-tooltip="true" />
      <!-- 状态 -->
      <el-table-column show-overflow-tooltip align="center" label="状态" prop="status">
        <!-- 帐号状态 0:正常 1:停用 -->
        <template slot-scope="{ row }">
          <!-- 帐号状态 0:正常 1:停用 -->
          <el-switch
            :disabled="!hasPermission('distribution:user:updateUserStatus')"
            v-model="row.status"
            :active-value="'0'"
            :inactive-value="'1'"
            @change="handleStatusChange(row)"
          />
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" label="是否管理员" prop="isAdmin">
        <!-- 是否管理员 0:否 1:是 -->
        <template slot-scope="{ row }">
          <div v-if="row.isAdmin == 1" style="color: red">{{ $t('是') }}</div>
          <div v-else style="color: blue">{{ $t('否') }}</div>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" :label="$t('操作')" align="center" width="200">
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.row)" v-hasPermi="['distribution:user:updatePlatformUser']">
            编辑
          </el-button>
          <!-- 管理员不能删除 -->

          <el-button
            :disabled="scope.row.isAdmin == 1"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['distribution:user:delUserByUserId']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 新增修改 -->
    <AddAndEditUser
      :show.sync="addAndEditUser.show"
      @getList="getList"
      :formData="addAndEditUser.formData"
      :type="addAndEditUser.type"
    />
    <!-- 角色管理 -->
    <RoleManage :show.sync="roleManage.show" @getList="getList" />
  </div>
</template>

<script>
import { getPlatformUserList, delUserByUserId } from '@/api/platform/platformUsers'
import AddAndEditUser from './components/addAndEditUser.vue'
import { checkPermi } from '@/utils/permission'
import { updateUserStatus } from '@/api/platform/supplierManage'
import RoleManage from './components/roleManage/index.vue'

export default {
  activated() {
    this.$nextTick(() => this.$refs['TableRef']?.doLayout())
  },
  name: 'SkuConsumablePurchase',
  components: { AddAndEditUser, RoleManage },
  data() {
    return {
      /** 角色管理 */
      roleManage: {
        show: false,
      },
      /**新增/修改 用户 */
      addAndEditUser: {
        show: false,
        type: '',
        formData: {},
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 商品SKU耗材采购表格数据
      tableData: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: '',
        nickName: '',
        phonenumber: '',
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    hasPermission(str) {
      return checkPermi([str])
    },
    // 帐号状态 status  0:正常 1:停用
    handleStatusChange(row) {
      const data = {
        ...row,
        targetRoleId: row.roleId,
      }
      console.log(`data ==>`, JSON.parse(JSON.stringify(data)))
      updateUserStatus(data)
        .then((res) => {
          const { code, msg } = res
          if (code === 200) {
            this.$message({ type: 'success', message: msg })
            this.getList()
          }
        })
        .catch(() => {
          row.status == '0' ? (row.status = '1') : (row.status = '0')
        })
    },
    handleAdd() {
      this.addAndEditUser.type = 'add'
      this.addAndEditUser.show = true
    },
    handleEdit(row) {
      this.addAndEditUser.type = 'edit'
      this.addAndEditUser.formData = row
      this.addAndEditUser.show = true
    },
    handleDelete(row) {
      this.$confirm(this.$t('确认删除?'), this.$t('提示'), {
        type: 'warning',
      }).then(() => {
        const data = {
          id: row.userId,
          distrId: JSON.parse(localStorage.getItem('distr') || '{}').distrId,
        }
        delUserByUserId(data).then((res) => {
          this.$message({ type: 'success', message: res.msg })

          this.getList()
        })
      })
    },
    /** 查询商品SKU耗材采购列表 */
    getList() {
      this.loading = true
      getPlatformUserList(this.queryParams).then((response) => {
        this.tableData = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 表单重置
    reset() {
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
  },
}
</script>
